package com.oitsm.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {
	/**
	 * 毫秒
	 */
	public final static int TYPE_MS = 4;
	/**
	 * 到秒
	 */
	public final static int TYPE_SECOND = 0;
	/**
	 * 到分
	 */
	public final static int TYPE_MIN = 1;
	/**
	 * 到小时
	 */
	public final static int TYPE_HOUR = 2;
	/**
	 * 到天
	 */
	public final static int TYPE_DAY = 3;
	/**
	 * 到月
	 */
	public final static int TYPE_MONTH = 5;
	
	
	private static SimpleDateFormat secFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat minFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static SimpleDateFormat hourFormat = new SimpleDateFormat("yyyy-MM-dd HH");
	private static SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");

	
	public static String format(Date date,int type){
		if(type == TYPE_SECOND){
			return format(date,secFormat);
		}else if(type == TYPE_MIN){
			return format(date,minFormat);
		}else if(type == TYPE_HOUR){
			return format(date,hourFormat);
		}else if(type == TYPE_DAY){
			return format(date,dayFormat);
		}else if(type == TYPE_MONTH){
			return format(date,monthFormat);
		}
		return format(date,secFormat);
	}
	
	public static String format(long date,int type){
		if(type == TYPE_SECOND){
			return format(date,secFormat);
		}else if(type == TYPE_MIN){
			return format(date,minFormat);
		}else if(type == TYPE_HOUR){
			return format(date,hourFormat);
		}else if(type == TYPE_DAY){
			return format(date,dayFormat);
		}else if(type == TYPE_MONTH){
			return format(date,monthFormat);
		}
		return format(date,secFormat);
	}
	
	public static Timestamp format(String timestr, int type) {
		Timestamp formatTime = null;
		try {
			switch (type) {
			case TYPE_SECOND:
				formatTime = new Timestamp(secFormat.parse(timestr).getTime());
				break;
			case TYPE_MIN:
				formatTime = new Timestamp(minFormat.parse(timestr).getTime());
				break;
			case TYPE_HOUR:
				formatTime = new Timestamp(hourFormat.parse(timestr).getTime());
				break;
			case TYPE_DAY:
				formatTime = new Timestamp(dayFormat.parse(timestr).getTime());
				break;
			case TYPE_MONTH:
				formatTime = new Timestamp(monthFormat.parse(timestr).getTime());
				break;
			default:
				formatTime = new Timestamp(secFormat.parse(timestr).getTime());
			}
			return formatTime;
		} catch (ParseException e) {
			return formatTime;
		}
	}
	public static String format(Date date){
		return format(date,secFormat);
	}
	
	public static String format(Date date,SimpleDateFormat formater){
		if(date == null){
			return null;
		}
		return formater.format(date);
	}
	
	private static String format(long date,SimpleDateFormat formater){
		return formater.format(date);
	}
	
	public static String formatNotNull(Date date){
		return formatNotNull(date,secFormat);
	}
	
	public static String formatNotNull(Date date,SimpleDateFormat formater){
		if(date == null){
			return "";
		}
		return formater.format(date);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
